package org.example;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class CSVToDatabaseGUI extends JFrame {
    private JTextField csvPathField;
    private JTextField jdbcURLField;
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JTextField columnCountField;
    private JTextField tableNameField;

    public CSVToDatabaseGUI() {
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        // Create a panel for CSV file path
        JPanel csvPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        csvPathField = new JTextField(20);
        JButton chooseButton = new JButton("选择");
        chooseButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                int returnValue = fileChooser.showOpenDialog(null);
                if (returnValue == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = fileChooser.getSelectedFile();
                    csvPathField.setText(selectedFile.getAbsolutePath());
                }
            }
        });

        csvPanel.add(new JLabel("CSV文件路径："));
        csvPanel.add(csvPathField);
        csvPanel.add(chooseButton);
        panel.add(csvPanel);

        // Create panels for other input fields
        panel.add(createInputPanel("JDBC URL：", jdbcURLField = new JTextField("jdbc:mysql://localhost:3306/crawler", 20)));
        panel.add(createInputPanel("用户名：", usernameField = new JTextField("root", 20)));
        panel.add(createInputPanel("密码：", passwordField = new JPasswordField(20)));
        panel.add(createInputPanel("列数：", columnCountField = new JTextField(20)));
        panel.add(createInputPanel("表名：", tableNameField = new JTextField(20)));

        // Create a panel for the buttons and set it to FlowLayout.CENTER
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JButton startButton = new JButton("开始上传");
        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CSVToDatabase csvToDatabase = new CSVToDatabase(
                        getCsvPath(),
                        getJdbcURL(),
                        getUsername(),
                        getPassword(),
                        getColumnCount(),
                        getTableName()
                );
                try {
                    csvToDatabase.execute();
                    JOptionPane.showMessageDialog(null, "数据上传成功");
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, "数据上传失败");
                }
            }
        });
        buttonPanel.add(startButton);

        JButton cancelButton = new JButton("取消");
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        buttonPanel.add(cancelButton);

        panel.add(buttonPanel);

        getContentPane().add(panel, BorderLayout.CENTER);
    }

    private JPanel createInputPanel(String label, JTextField textField) {
        JPanel inputPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        inputPanel.add(new JLabel(label));
        inputPanel.add(textField);
        return inputPanel;
    }

    public String getCsvPath() {
        return csvPathField.getText();
    }

    public String getJdbcURL() {
        return jdbcURLField.getText();
    }

    public String getUsername() {
        return usernameField.getText();
    }

    public String getPassword() {
        return new String(passwordField.getPassword());
    }

    public int getColumnCount() {
        return Integer.parseInt(columnCountField.getText());
    }

    public String getTableName() {
        return tableNameField.getText();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new CSVToDatabaseGUI().setVisible(true);
            }
        });
    }
}
